﻿@* Generator: Template TypeVisibility: Internal GeneratePrettyNames: true *@
@using System
@using System.Collections.Generic
@using Hangfire
@using Hangfire.Dashboard
@using Hangfire.Dashboard.Pages
@using Hangfire.Dashboard.Resources
@using Hangfire.States
@using Hangfire.Storage
@inherits RazorPage
@{
    Layout = new LayoutPage(Strings.RecurringJobsPage_Title);
	List<RecurringJobDto> recurringJobs;
    
    int from, perPage;

    int.TryParse(Query("from"), out from);
    int.TryParse(Query("count"), out perPage);

    Pager pager = null;

	using (var connection = Storage.GetConnection())
	{
	    var storageConnection = connection as JobStorageConnection;
	    if (storageConnection != null)
	    {
	        pager = new Pager(from, perPage, storageConnection.GetRecurringJobCount());
	        recurringJobs = storageConnection.GetRecurringJobs(pager.FromRecord, pager.FromRecord + pager.RecordsPerPage - 1);
	    }
	    else
	    {
            recurringJobs = connection.GetRecurringJobs();
	    }
	}
}

<div class="row">
    <div class="col-md-12">
        <h1 class="page-header">@Strings.RecurringJobsPage_Title</h1>
        @if (recurringJobs.Count == 0)
        {
            <div class="alert alert-info">
                @Strings.RecurringJobsPage_NoJobs
                @if (!IsReadOnly)
                {
                    <a class="btn btn-sm btn-default" href="@Url.To(ManagementPage.UrlRoute)">
                        <span class="glyphicon glyphicon-plus"></span>
                        @Strings.ResourceManager.GetString("添加一个新的计划任务")
                    </a>
                }
            </div>
        }
        else
        {
            <div class="js-jobs-list">
                <div class="btn-toolbar btn-toolbar-top">
                    @if (!IsReadOnly)
                    {
                        <button class="js-jobs-list-command btn btn-sm btn-primary"
                                data-url="@Url.To("/recurring/trigger")"
                                data-loading-text="@Strings.RecurringJobsPage_Triggering"
                                disabled="disabled">
                            <span class="glyphicon glyphicon-play-circle"></span>
                            @Strings.RecurringJobsPage_TriggerNow
                        </button>
                    }
                    @if (!IsReadOnly)
                    {
                        <button class="js-jobs-list-command btn btn-sm btn-warning"
                                data-url="@Url.To("/recurring/pause")"
                                data-loading-text="@Strings.ResourceManager.GetString("暂停中")"
                                data-confirm="@Strings.ResourceManager.GetString("您确定要暂停所选的全部作业吗？")"
                                disabled="disabled">
                            <span class="glyphicon glyphicon-pause"></span>
                            @Strings.ResourceManager.GetString("暂停")
                        </button>
                        <button class="js-jobs-list-command btn btn-sm btn-success"
                                data-url="@Url.To("/recurring/repeat")"
                                data-loading-text="@Strings.ResourceManager.GetString("恢复中")"
                                data-confirm="@Strings.ResourceManager.GetString("您确定要恢复所选的全部作业吗？")"
                                disabled="disabled">
                            <span class="glyphicon glyphicon-repeat"></span>
                            @Strings.ResourceManager.GetString("恢复")
                        </button>

                        <a class="btn btn-sm btn-default" href="@Url.To(ManagementPage.UrlRoute)">
                            <span class="glyphicon glyphicon-plus"></span>
                            @Strings.ResourceManager.GetString("添加")
                        </a>
                    }
                    @if (!IsReadOnly)
                    {
                        <button class="js-jobs-list-command btn btn-sm btn-default"
                                data-url="@Url.To("/recurring/remove")"
                                data-loading-text="@Strings.Common_Deleting"
                                data-confirm="@Strings.Common_DeleteConfirm"
                                disabled="disabled">
                            <span class="glyphicon glyphicon-remove"></span>
                            @Strings.Common_Delete
                        </button>
                    }
                    @if (pager != null)
                    {
                        @: @Html.PerPageSelector(pager)
                    }
                </div>

                <div class="table-responsive">
                    <table class="table">
                        <thead>
                            <tr>
                                @if (!IsReadOnly)
                                {
                                    <th class="min-width">
                                        <input type="checkbox" class="js-jobs-list-select-all" />
                                    </th>
                                }
                                <th class="min-width">@Strings.Common_Id</th>
                                <th class="min-width">@Strings.RecurringJobsPage_Table_Cron</th>
                                <th class="min-width">@Strings.RecurringJobsPage_Table_TimeZone</th>
                                <th>@Strings.Common_Job</th>
                                <th>@Strings.ServersPage_Table_Queues</th>
                                <th>状态</th>

                                <th class="align-right min-width">@Strings.RecurringJobsPage_Table_NextExecution</th>
                                <th class="align-right min-width">@Strings.RecurringJobsPage_Table_LastExecution</th>
                                <th class="align-right min-width">@Strings.Common_Created</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var job in recurringJobs)
                            {
                            <tr class="js-jobs-list-row hover">
                                @if (!IsReadOnly)
                                {
                                    <td>
                                        <input type="checkbox" class="js-jobs-list-checkbox" name="jobs[]" value="@job.Id" />
                                    </td>
                                }
                                <td class="min-width">@job.Id</td>
                                <td class="min-width">
                                    @* ReSharper disable once EmptyGeneralCatchClause *@
                                    @{
                                        string cronDescription = null;
                                        bool cronError = false;

                                        if (!String.IsNullOrEmpty(job.Cron))
                                        {
                                            try
                                            {
                                                RecurringJobEntity.ParseCronExpression(job.Cron);
                                            }
                                            catch (Exception ex)
                                            {
                                                cronDescription = ex.Message;
                                                cronError = true;
                                            }

                                            if (cronDescription == null)
                                            {
#if FEATURE_CRONDESCRIPTOR
                                                                        try
                                                                        {
                                                                            cronDescription = CronExpressionDescriptor.ExpressionDescriptor.GetDescription(job.Cron);
                                                                        }
                                                                        catch (FormatException)
                                                                        {
                                                                        }
#endif
                                            }
                                        }
                                    }

                                    @if (cronDescription != null)
                                    {
                                        <code title="@cronDescription">
                                            @if (cronError)
                                            {
                                                <span class="glyphicon glyphicon-exclamation-sign"></span>
                                            }
                                            @job.Cron
                                        </code>
                                    }
                                    else
                                    {
                                        <code>@job.Cron</code>
                                    }
                                </td>
                                <td class="min-width">
                                    @if (!String.IsNullOrWhiteSpace(job.TimeZoneId))
                                    {
                                        string displayName;
                                        string standardName;
                                        Exception exception = null;

                                        try
                                        {
                                            var resolver = DashboardOptions.TimeZoneResolver ?? new DefaultTimeZoneResolver();
                                            var timeZone = resolver.GetTimeZoneById(job.TimeZoneId);
                                            displayName = timeZone.DisplayName;
                                            standardName = timeZone.StandardName;
                                        }
                                        catch (Exception ex)
                                        {
                                            displayName = null;
                                            standardName = null;
                                            exception = ex;
                                        }

                                        <span title="@displayName" data-container="body">@standardName
                                        @if (exception != null)
                                        {
                                            <span class="glyphicon glyphicon-exclamation-sign" title="@exception.Message"></span>
                                        }
                                    </span>
                                }
                                else
                                {
                                    @: UTC
                                }
                                </td>
                                <td class="word-break">
                                    @if (job.Job != null)
                                    {
                                        @: @Html.JobName(job.Job)
                                    }
                                    else if (job.LoadException != null && job.LoadException.InnerException != null)
                                    {
                                        <em>@job.LoadException.InnerException.Message</em>
                                    }
                                    else if (job.LoadException != null)
                                    {
                                        <em>@job.LoadException.Message</em>
                                    }
                                    else
                                    {
                                        <em>@Strings.Common_NotAvailable</em>
                                    }
                                </td>
                                <td class="word-break">
                                    @Html.QueueLabel(job.Queue)
                                </td>
                                <td class="word-break">
                                    @if (job.PauseState)
                                    {
                                        <span class="label label-warning">@Strings.ResourceManager.GetString("暂停")</span>
                                    }
                                    else
                                    {
                                        <span class="label label-success">@Strings.ResourceManager.GetString("正常")</span>
                                    }
                                </td>
                                <td class="align-right min-width">
                                    @if (job.NextExecution != null)
                                    {
                                        @Html.RelativeTime(job.NextExecution.Value)
                                    }
                                    else
                                    {
                                        if (!String.IsNullOrEmpty(job.Error))
                                        {
                                            <span class="label label-danger text-uppercase" title="@job.Error">@Strings.Common_Error</span>
                                        }
                                        else
                                        {
                                            <span class="label label-warning text-uppercase" title="@Strings.RecurringJobsPage_RecurringJobDisabled_Tooltip">@Strings.Common_Disabled</span>
                                        }
                                    }
                                </td>
                                <td class="align-right min-width">
                                    @if (job.LastExecution != null)
                                    {
                                        if (!String.IsNullOrEmpty(job.LastJobId))
                                        {
                                            <a href="@Url.JobDetails(job.LastJobId)" style="text-decoration: none">
                                                <span class="label label-default label-hover" style="@($"background-color: {JobHistoryRenderer.GetForegroundStateColor(job.LastJobState ?? EnqueuedState.StateName)};")">
                                                    @Html.RelativeTime(job.LastExecution.Value)
                                                </span>
                                            </a>
                                        }
                                        else
                                        {
                                            <em>
                                                @Strings.RecurringJobsPage_Canceled @Html.RelativeTime(job.LastExecution.Value)
                                            </em>
                                        }
                                    }
                                    else
                                    {
                                        <em>@Strings.Common_NotAvailable</em>
                                    }
                                </td>
                                <td class="align-right min-width">
                                    @if (job.CreatedAt != null)
                                    {
                                        @Html.RelativeTime(job.CreatedAt.Value)
                                    }
                                    else
                                    {
                                        <em>N/A</em>
                                    }
                                </td>
                            </tr>
                             }
                        </tbody>
                    </table>
                </div>

                @if (pager != null)
                {
                    @: @Html.Paginator(pager)
                }
            </div>
        }
    </div>
</div>    